import sys


sys.setrecursionlimit(10**5 )

class Edge:
    def __init__(self, v: int, nxt: int):
        self.v = v
        self.nxt = nxt

N = 200005
E = [Edge(0, 0) for _ in range(N << 1)]
h = [0] * N
val = [0] * N
idx = 2
vis = [0] * N

def addEdge(u: int, v: int) -> None:
    global idx,E,h
    E[idx] = Edge(v, h[u])
    h[u] = idx
    idx += 1


stk = []
dfn = [0] * N
low = [0] * N
scc = [0] * N
su = [0] * N
cscc = 0


def Tarjan(u: int, frm: int) -> None:
    global idx, cscc,stk,dfn,low
    idx += 1
    dfn[u] = low[u] = idx
    stk.append(u)
    i = h[u]
    while i != 0:
        v = E[i].v
        if dfn[v] == 0:
            Tarjan(v, i)
            low[u] = min(low[u], low[v])
        elif (i ^ 1) != frm:
            low[u] = min(low[u], dfn[v])
        i = E[i].nxt

    if low[u] == dfn[u]:
        top = -1
        cscc += 1
        while top != u:
            top = stk.pop()
            scc[top] = cscc


def calc(u: int, g: list[list[int]]) -> None:
    global ans, tot,su,vis
    vis[u] = True
    for v in g[u]:
        if not vis[v]:
            calc(v, g)
            su[u] += su[v]
    ans = min(ans, abs(tot - su[u] * 2))


n, m = map(int, input().split())

val_input = input().split()

for i in range(1, n+1):
    val[i] = int(val_input[i-1])


for _ in range(m):
    u, v = map(int, input().split())
    addEdge(u, v)
    addEdge(v, u)

for i in range(1, n + 1):
    if dfn[i] == 0:
        Tarjan(i, 0)

g = [[] for _ in range(cscc + 1)]

for u in range(1, n + 1):
    su[scc[u]] += val[u]
    i = h[u]
    while i != 0:
    # for i in range(h[u], 0, -1):
        v = E[i].v
        if scc[u] != scc[v]:
            g[scc[u]].append(scc[v])
            g[scc[v]].append(scc[u])
        i = E[i].nxt
ans = float('inf')
tot = sum(val)

if cscc != 1:
    calc(1, g)
else:
    ans = -1

print(ans)